﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebsiteDataModel;
using CMSModel;
using System.Globalization;
using BLL;

public partial class news_CreateData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateData();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        CreateData();
    }

    void CreateData()
    {
        CommonBll cbll = new CommonBll();
        WebsiteDataEntities dal = new WebsiteDataEntities();
        string[] codes = CommonBll.DicStocks.Keys.ToArray();
        foreach (var code in codes)
        {
            var data = dal.BData.Where(x => x.StockId == code).GroupBy(x => x.Oid).Select(x => x.Key);
            if (data.Count() > 0)
            {
                var oids = data.ToList();
                foreach (var c in oids)
                {
                    AddTzData(code, c);
                }
            }
        }
    }

    public void AddTzData(string code,string oid)
    {
        try
        {
            WebsiteDataEntities dal = new WebsiteDataEntities();
            var data = dal.BData.Where(x => x.Oid == oid && x.StockId == code).OrderByDescending(x => x.CDate).Take(2).ToList();
            if (PJ[data[0].InvestmentGrade] > PJ[data[1].InvestmentGrade])
            {
                var id = data[0].id;
                if (dal.tzData.Any(x => x.oid == oid && x.stock == code && x.bid ==id))
                {
                    return;
                }
                else
                {
                    tzData tz = new tzData();
                    tz.oid = oid;
                    tz.spj = data[1].InvestmentGrade;
                    tz.bid = data[0].id;
                    tz.stock = code;
                    dal.tzData.AddObject(tz);
                    dal.SaveChanges();
                }
            }
        }
        catch
        {
        }
    }

    public Dictionary<string, int> PJ
    {
        get
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();
            dic.Add("卖出", -1);
            dic.Add("不评级", 0);
            dic.Add("中性", 1);
            dic.Add("增持", 2);
            dic.Add("买入", 3);
            return dic;
        }
    }
    protected void btnTzbw_Click(object sender, EventArgs e)
    {
        TzbwBLL bll = new TzbwBLL();
        bll.Import();
    }
    protected void btnGSGK_Click(object sender, EventArgs e)
    {
        CompanyOverviewImport c = new CompanyOverviewImport();
        c.Import();
    }
}